package me.stone.stanimeclient.core.ui.item

import android.annotation.SuppressLint
import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.appcompat.content.res.AppCompatResources
import kotlinx.coroutines.flow.StateFlow
import me.stone.stanimeclient.core.common.extensions.sp
import me.stone.stanimeclient.core.ui.R
import kotlin.math.roundToInt

interface IToggleText {
    val label: StateFlow<CharSequence>
    val state: StateFlow<Boolean>
    fun toggle()

    @SuppressLint("NewApi")
    fun Context.buildToggleText(
        @StringRes strResId: Int,
        @DrawableRes iconResId: Int,
        iconSize: Int = 14f.sp.roundToInt()
    ): CharSequence {
        val strContent = getString(strResId)
        val drawable = AppCompatResources.getDrawable(this, iconResId)?.apply {
            setBounds(0, 0, iconSize, iconSize)
            setTint(getColor(R.color.md_theme_primary))
        } ?: return SpannableString(strContent)
        return SpannableString("$strContent X").apply {
            setSpan(
                CenteredImageSpan(drawable),
                strContent.length + 1, strContent.length + 2,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
    }
}